package www.wen.com.common.lib.base.handler

import android.os.Handler
import android.os.Looper
import android.os.Message
import androidx.fragment.app.Fragment
import com.orhanobut.logger.Logger
import java.lang.ref.WeakReference

/**
 * @author WEN
 * @Description:
 * @date 2021/7/26 17:50
 */
abstract class BaseFragmentHandler<T : Fragment>(var fragment: T): Handler(Looper.getMainLooper()) {

    private var mWeakReference: WeakReference<T?> = WeakReference(fragment)

    override fun handleMessage(msg: Message) {
        if (mWeakReference.get() == null || mWeakReference.get()!!.isRemoving) {
            // 确认Fragment是否不可用
            Logger.i("Fragment is gone")
        } else {
            handleMessage(msg, msg.what, mWeakReference.get()!!)
        }
    }

    abstract fun handleMessage(msg: Message, what: Int, t: T)

}